package com.thin

import com.android.build.api.transform.*
import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.internal.pipeline.TransformManager
import org.gradle.api.Plugin
import org.gradle.api.Project

class TT2Plugin extends Transform implements Plugin<Project> {

    @Override
    void apply(Project project) {

        project.task("tico", {
            print("TT2Plugin task exec")
        })

        if (isAndroid(project)) {
            AppExtension android = project.extensions.getByType(AppExtension)
            android.registerTransform(this)
        } else throw IllegalStateException("tico plugin err:only support android module")
    }

    static def isAndroid(Project project) {
        AppPlugin app = project.plugins.findPlugin(AppPlugin)
        return app != null
    }

    @Override
    void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
//        super.transform(transformInvocation)

        //clear last time cache

        outputProvider = transformInvocation.outputProvider
        if (outputProvider != null)
            outputProvider.deleteAll()

        transformInvocation.inputs.each { TransformInput input ->

            input.directoryInputs.each {
                DirectoryInput directoryInput ->
                    directoryInput.file.eachFileRecurse {
                        File file ->

                    }

            }

        }
    }

    @Override
    String getName() {
        return "ticoTransform"
    }

    @Override
    Set<QualifiedContent.ContentType> getInputTypes() {
        return TransformManager.CONTENT_CLASS
    }

    @Override
    Set<? super QualifiedContent.Scope> getScopes() {
        return TransformManager.SCOPE_FULL_PROJECT
    }

    @Override
    boolean isIncremental() {
        return false
    }
}